﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace KgUtils.Wpf.BaseClasses
{
    public class Presenter<TEntityBE> : IPresenter
        where TEntityBE : BaseBE<TEntityBE>, IEntity
    {
        public IBusiness<TEntityBE> BusinessBS { get; private set; }
        public IView<TEntityBE> View { get; private set; }

        public Presenter(IView<TEntityBE> pView, IBusiness<TEntityBE> pBusiness)
        {
            View = pView;
            BusinessBS = pBusiness;
        }

        public void ChargerDonneesDansListe(IEntity param)
        {
            View.ListeEntites = new ObservableCollection<TEntityBE>(BusinessBS.ObtenirDonnees(param));
        }

        public bool SauvegarderEntiteCourante()
        {
            if (IsDataControlOK())
            {
                return BusinessBS.Sauvegarder(View.EntiteCourante);
            }
            return false;
        }

        public bool SauvegarderListeEntites()
        {
            if (IsDataControlOK())
            {
                return BusinessBS.SauvegarderListeEntites(View.ListeEntites.ToList());
            }
            return false;
        }

        /// <summary>
        /// Vérifie la fiabilité des données
        /// </summary>
        /// <returns></returns>
        public bool IsDataControlOK()
        {
            return View.IsDataControlOK();
        }
    }
}
